package com.dewod.producesales.activity.setting;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.dewod.producesales.R;
import com.dewod.producesales.utils.SharedUtil;

public class WorkModeActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private Button btnConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_mode);

        radioGroup = findViewById(R.id.radioGroup);
        btnConfirm = findViewById(R.id.btn_confirm);

        // 从 strings.xml 加载工作模式列表
        String[] workModes = getResources().getStringArray(R.array.workMode);

        // 动态添加 RadioButton
        for (int i = 0; i < workModes.length; i++) {
            RadioButton radioButton = new RadioButton(this);
            radioButton.setText(workModes[i]);
            radioButton.setId(i); // 使用索引作为 ID
            radioGroup.addView(radioButton);

            // 设置默认选中项
            if (workModes[i].equals(SharedUtil.getInstance(this).readString("WORK_MODE", workModes[0]))) {
                radioGroup.check(i);
            }
        }

        // 确认按钮点击事件
        btnConfirm.setOnClickListener(v -> {
            int selectedId = radioGroup.getCheckedRadioButtonId();
            if (selectedId != -1) {
                RadioButton selected = findViewById(selectedId);
                String selectedMode = selected.getText().toString();

                // 保存到 SharedUtil
                SharedUtil.getInstance(this).writeString("WORK_MODE", selectedMode);

                // 提示用户并返回
                Toast.makeText(this, "已设置为：" + selectedMode, Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "请选择一个工作模式", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
